package cn.liujianbo.example.validate.controller;

import cn.liujianbo.example.validate.controller.base.BaseCrud;
import cn.liujianbo.example.validate.entity.user.User;
import cn.liujianbo.example.validate.entity.user.UserSearch;
import cn.liujianbo.example.validate.result.R;
import cn.liujianbo.example.validate.valid.ValidateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * @author 刘建波
 * @date 2020-12-24 14:39
 */
@RestController
@RequestMapping("/user")
@Validated
public class UserController implements BaseCrud<User, UserSearch> {
    @Override
    public R add(User user) {
        return R.ok();
    }

    @Override
    public R del(Long id) {
        return R.ok();
    }

    @Override
    public R update(Long id, User user) {
        return R.ok();
    }

    @Override
    public R selectById(String id) {
        return R.ok();
    }

    @Override
    public R selectList(UserSearch userSearch) {
        return R.ok();
    }

    @GetMapping("{id}/{name}")
    public R updateName(@PathVariable Long id, @NotBlank(message = "名字不能为空！") @PathVariable String name) {
        return R.ok();
    }

    /**
     * 无法校验List中的元素，自己封装一个实体再嵌套校验
     * @param list
     * @return
     */
    @PostMapping("addAll")
    public R addBatch(@Validated(ValidateGroup.Add.class) @Size(min = 1, message = "用户信息未传递") @RequestBody List<User> list) {
        return R.ok();
    }

}
